<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        img{
            width: 50px;
            height: 50px;
        }
        .td{
            width: 40px;height: 20px;
            text-align: center;
        }
        td span{
            cursor: pointer;
        }
    </style>
</head>
<body>
    <table id='all'>
             
    </table>
    <script src="jquery-1.12.4.min.js"></script>
    <script>
        $('#all').on('click','.add',function(){
            var id = $(this).parents('tr').children().eq(0).text();
            $.ajax({
                url:"http://localhost/interface/update.php",
                data:{
                    id:id,
                    type:'add'
                },
                success:function(res){
                    if(res.code===1){
                        show()
                    }
                },
                dataType:"json"
            })
        })
        $('#all').on('click','.cut',function(){
            var id = $(this).parents('tr').children().eq(0).text();
            $.ajax({
                url:"http://localhost/interface/update.php",
                data:{
                    id:id,
                    type:'cut'
                },
                success:function(res){
                    if(res.code===1){
                        show()
                    }
                },
                dataType:"json"
            })
        })
        // 删除商品
        $('#all').on('click','.del',function(){
            var id = $(this).parents('tr').children().eq(0).text();
            $.ajax({
                url:"http://localhost/interface/del.php",
                data:{
                    id:id
                },
                success:function(res){
                    if(res.code===1){
                        show();
                    }
                },
                dataType:"json"
            })
        })




        // 展示购物车数据
        show()
        function show(){
            $.ajax({
            url:"http://localhost/interface/showlist.php",
            success:function(res){
                // console.log(res)
                $('#all').html(`<tr>
                    <th>编号</th>
                    <th>商品名</th>
                    <th>图片</th>
                    <th>价格</th>
                    <th>数量</th>
                    <th>删除</th>
                </tr>   `)
                if(res.code==1){
                    res.data.forEach(function(val){
                        $('#all').append(`<tr>
            <td>${val.product_id}</td>
            <td style="width:200px;">${val.product_name}</td>
            <td>
                <img src="${val.product_img}" alt="">
            </td>
            <td>
                ${val.product_price}
            </td>
            <td>
                <span class="cut">-</span>
                <input type="text" value='${val.product_num}' disabled class="td">
                <span class="add">+</span>
            </td>
            <td>
                <input type="button" value="删除" class="del">
            </td>
        </tr>`)
                    })
                }
            },
            dataType:"json"
        })
        }
    </script>
</body>
</html>